------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    突破界面

]]--
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 控件
    local Widget = require "widgets/widget"
    local Image = require "widgets/image"
    local UIAnim = require "widgets/uianim"
    local Screen = require "widgets/screen"
    local AnimButton = require "widgets/animbutton"
    local ImageButton = require "widgets/imagebutton"
    local Menu = require "widgets/menu"
    local Text = require "widgets/text"
    local TEMPLATES = require "widgets/redux/templates"
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- image api
    local BASE_ATLAS = "images/widgets/hutao_inspect_hud.xml"
    local BUFF_ICON_ATLAS = "images/widgets/hutao_talent_buff_icon.xml"
    local function get_image(image_name)
        return TUNING.HUTAO_INSPECT_HUD_FNS.get_image(image_name)
    end
    local function get_info(index)
        return TUNING.HUTAO_INSPECT_HUD_FNS.get_info(index)
    end
    local move_debug_event_install = function()    end
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 挂载函数，方便动态修改
    TUNING.HUTAO_INSPECT_HUD_FNS.create_ascension_page = function(inst,root)            
        ----------------------------------------------------------------------
        --- page 节点
            local page = root:AddChild(Widget())
            page:Hide()
            root.inst:ListenForEvent("page_display",function(_,cmd)
                if cmd == "display_ascension_page" then
                    page:Show()
                else
                    page:Hide()
                end
            end)
        ----------------------------------------------------------------------
        --- level box
            local level_box = page:AddChild(Image(BASE_ATLAS,"page_ascension_level_box.tex"))
            level_box:SetPosition(-526,242)
            -- move_debug_event_install(level_box,root)
            local level_text = level_box:AddChild(Text(CODEFONT,60,"00",{ 0/255 , 0/255 ,0/255 , 1}))
            level_text:SetPosition(20,0)
            local function level_update_fn()
                local current_level = inst.replica.hutao_com_level_sys:GetLevel()
                level_text:SetString(tostring(current_level))
                if inst.replica.hutao_com_level_sys:CanLevelUp() then
                    level_text:SetColour({0/255,0/255,0/255,1})
                else
                    level_text:SetColour({255/255,0/255,0/255,1})
                end
            end
            level_update_fn()
            page.inst:ListenForEvent("hutao_com_level_sys_update",level_update_fn,inst)
        ----------------------------------------------------------------------
        --- info box
            local info_box = page:AddChild(Image(BASE_ATLAS,"page_ascension_info_box.tex"))
            info_box:SetPosition(0,-300)
            -- move_debug_event_install(info_box,root)
            local info_text = info_box:AddChild(Text(CODEFONT,60,"",{ 0/255 , 0/255 ,0/255 , 1}))
            page.inst:ListenForEvent("set_info",function(_,index)                    
                local pt,str,size = get_info(index)
                if pt and str and size then
                    info_text:SetString(str)
                    info_text:SetPosition(pt.x,pt.y)
                    info_text:SetSize(size)
                else
                    info_text:SetString("")
                end
            end)
            page.inst:ListenForEvent("clear_info",function()
                info_text:SetString("")
            end)
        ----------------------------------------------------------------------
        --- help button
            local help_atlas,help_img = get_image("page_ascension_button_help")
            local help_button = page:AddChild(ImageButton(help_atlas,help_img,help_img,help_img,help_img,help_img))
            help_button:SetPosition(525,242)
            help_button.focus_scale = {1,1,1}
            help_button.ongainfocus = function()
                page.inst:PushEvent("set_info","page_ascension_button_help")
            end
            help_button.onlosefocus = function()
                page.inst:PushEvent("clear_info")
            end
        ----------------------------------------------------------------------
        --- 骨灰盒子
            local bone_box_data = {
                [1] = {Vector3(-420,0,0),"page_ascension_memorial_tablet_1.tex"},
                [2] = {Vector3(-285,100,0),"page_ascension_memorial_tablet_2.tex"},
                [3] = {Vector3(-150,0,0),"page_ascension_memorial_tablet_3.tex"},
                [4] = {Vector3(0,100,0),"page_ascension_memorial_tablet_4.tex"},
                [5] = {Vector3(150,0,0),"page_ascension_memorial_tablet_5.tex"},
                [6] = {Vector3(285,100,0),"page_ascension_memorial_tablet_6.tex"},
                [7] = {Vector3(420,0,0),"page_ascension_memorial_tablet_7.tex"},
            }
            local bone_box_root = page:AddChild(Widget())
            bone_box_root:SetPosition(0,15)
            local bone_box_buttons = {}
            for index, data in pairs(bone_box_data) do
                local temp_button = bone_box_root:AddChild(ImageButton(BASE_ATLAS,
                    "page_ascension_memorial_tablet_0.tex",
                    "page_ascension_memorial_tablet_0.tex",
                    "page_ascension_memorial_tablet_0.tex",
                    "page_ascension_memorial_tablet_0.tex",
                    "page_ascension_memorial_tablet_0.tex"
                ))
                temp_button:SetPosition(data[1].x,data[1].y)
                temp_button.focus_scale = {1.01,1.01,1.01}
                -- table.insert(bone_box_buttons,temp_button)
                bone_box_buttons[index] = temp_button
                -- move_debug_event_install(temp_button,root)
                -- temp_button:SetOnClick(function()
                --     print("click ",index)
                -- end)
                temp_button.ongainfocus = function()
                    page.inst:PushEvent("set_info","page_ascension_memorial_tablet_"..index)
                    temp_button:MoveToFront()
                end
                -- temp_button.onlosefocus = function()
                --     page.inst:PushEvent("clear_info")
                -- end
            end
            local function bone_box_button_update_fn()
                for index, v in pairs(bone_box_buttons) do
                    if index%2 == 0 then
                        bone_box_buttons[index]:MoveToBack()
                    else
                        bone_box_buttons[index]:MoveToFront()
                    end
                end
                -- local current_level = inst.replica.hutao_com_level_sys:GetLevel()
                local level_up_unlocked_data = inst.replica.hutao_com_level_sys:GetData("level_up_unlocked") or {}
                local offset_y = 40  --- 变成牌子的时候坐标偏移
                for index, lv in pairs(level_up_unlocked_data) do
                    if lv then
                        bone_box_buttons[index]:SetTextures(
                            BASE_ATLAS,
                            bone_box_data[index][2],
                            bone_box_data[index][2],
                            bone_box_data[index][2],
                            bone_box_data[index][2],
                            bone_box_data[index][2]
                        )
                        bone_box_buttons[index]:SetPosition(bone_box_data[index][1].x,bone_box_data[index][1].y+offset_y)
                    end
                end
                ------- 49级的时候
                if inst.replica.hutao_com_level_sys:GetLevel() >= 49 then
                    local index = 7
                    bone_box_buttons[index]:SetPosition(bone_box_data[index][1].x,bone_box_data[index][1].y+offset_y)
                    bone_box_buttons[index]:SetTextures(
                        BASE_ATLAS,
                        bone_box_data[index][2],
                        bone_box_data[index][2],
                        bone_box_data[index][2],
                        bone_box_data[index][2],
                        bone_box_data[index][2]
                    )
                end
                -- print("66666",inst.replica.hutao_com_level_sys:GetLevel())
            end
            bone_box_button_update_fn()
            page.inst:ListenForEvent("hutao_com_level_sys_update",bone_box_button_update_fn,inst)
        ----------------------------------------------------------------------
        --- 突破按钮
            local button_ascension_atlas ,button_ascension_img = get_image("page_ascension_button_ascension")
            local button_ascension = page:AddChild(ImageButton(button_ascension_atlas,
                button_ascension_img,
                button_ascension_img,
                button_ascension_img,
                button_ascension_img,
                button_ascension_img
            ))
            button_ascension:SetPosition(0,-128)
            button_ascension:SetOnClick(function()
                page.inst:PushEvent("level_up_break_warning_box_display")
            end)
        ----------------------------------------------------------------------
        --- 警告BOX
            local warning_box = page:AddChild(Image(BASE_ATLAS,"page_ascension_ex_box_background.tex"))
            warning_box:SetPosition(0,0)
            warning_box:Hide()
            --- 关闭按钮
            local warning_box_close_button = warning_box:AddChild(ImageButton(BASE_ATLAS,
                "page_ascension_ex_box_button_close.tex",
                "page_ascension_ex_box_button_close.tex",
                "page_ascension_ex_box_button_close.tex",
                "page_ascension_ex_box_button_close.tex",
                "page_ascension_ex_box_button_close.tex"
            ))
            warning_box_close_button:SetPosition(300,210)
            warning_box_close_button:SetOnClick(function()
                warning_box:Hide()
            end)
            --- 确认按钮
            local warning_box_yes_button_atlas,warning_box_yes_button_img = get_image("page_ascension_ex_box_button_yes")
            local warning_box_yes_button = warning_box:AddChild(ImageButton(warning_box_yes_button_atlas,
                warning_box_yes_button_img,
                warning_box_yes_button_img,
                warning_box_yes_button_img,
                warning_box_yes_button_img,
                warning_box_yes_button_img
            ))
            warning_box_yes_button:SetPosition(0,-140)
            warning_box_yes_button:SetOnClick(function()
                inst.replica.hutao_com_level_sys:LevelUpBreak()
                warning_box:Hide()
                button_ascension:SetClickable(false)
                button_ascension.inst:DoTaskInTime(2,function()
                    button_ascension:SetClickable(true)
                end)
            end)
            --- info
            local warning_box_info = warning_box:AddChild(Text(CODEFONT,60,"",{ 0/255 , 0/255 ,0/255 , 1}))
            --- 打开窗口
            page.inst:ListenForEvent("level_up_break_warning_box_display",function()
                local pt,str,size = get_info("level_up_break_warning_info")
                if pt and str and size then
                    warning_box_info:SetPosition(pt.x,pt.y)
                    warning_box_info:SetString(str)
                    warning_box_info:SetSize(size)
                end
                warning_box:Show()
            end)
        ----------------------------------------------------------------------
        ---
            return page
        ----------------------------------------------------------------------
    end